//
//  TMCourseReserveController.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/7/17.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMCourseReserveController.h"
#import "TMCourseRemainCell.h"
#import "TMMyCourseModel.h"
#import "TMUserViewModel.h"
#import "TMCommodityDetailController.h"
@interface TMCourseReserveController ()
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) WZNoResultView *noResultView;
@end

@implementation TMCourseReserveController
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.tableView addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上课提醒";
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor hx_colorWithHexRGBAString:@"#F8F8F8"];
    self.viewModel = [[TMUserViewModel alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.viewModel getCourseOpenRemaindSuccessSuccess:^(id responseObject) {
            self.listArray = responseObject;
            [self noResultViewShowORNot];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        } Failed:^(NSString *error) {
            [self noResultViewShowORNot];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TMCourseRemainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMCourseRemainCell" forIndexPath:indexPath];
    cell.model = self.listArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 109;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMMyCourseModel * model = self.listArray[indexPath.row];
    TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
    vc.rid = model.cid;
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
